package com.manlost.biz.baby.impl.dao.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.manlost.biz.baby.impl.dao.interfaces.BabyDAO;
import com.manlost.biz.baby.model.dataobject.Baby;
import com.manlost.biz.baby.query.BabyQuery;
import com.manlost.biz.common.ibatis.BaseSqlMapClientDaoSupport;
import com.manlost.biz.common.ibatis.PageQueryList;

/**
 * 数据访问对象实现类
 * 
 * @wqq 2012-03-26
 */
@Repository
public class BabyDAOImpl extends BaseSqlMapClientDaoSupport implements BabyDAO {

	/**
	 * 插入数据
	 * 
	 * @param manlostBaby
	 * @return 插入数据的主键
	 */
	public Long insertOne(Baby baby) {
		Object id = getSqlMapClientTemplate().insert("baby.insert",
				baby);
		return (Long) id;
	}

	/**
	 * 更新记录
	 * 
	 * @param manlostBaby
	 * @return 受影响的行数
	 */
	public Integer updateOne(Baby baby) {
		int result = getSqlMapClientTemplate().update("baby.update",
				baby);
		return result;
	}

	/**
	 * 获取对象列表
	 * 
	 * @param manlostBabyQuery
	 * @return 对象列表
	 */
	@SuppressWarnings("unchecked")
	public PageQueryList<Baby> findListByQuery(
			BabyQuery babyQuery) {
		/*PageQueryList<ManlostBaby> queryList = new PageQueryList<ManlostBaby>(
				manlostBabyQuery);
		long totalCount = ((Long) getSqlMapClientTemplate().queryForObject(
				"baby.countBabysForQuery", manlostBabyQuery))
				.intValue();
		queryList.setTotalCount(totalCount);

		if (totalCount > 0) {
			List<ManlostBaby> items = getSqlMapClientTemplate().queryForList(
					"baby.findBabysForQuery", manlostBabyQuery);

			if (items != null) {
				queryList.addAll(items);
			}
		}*/

		return getSqlMapClientTemplate().queryForPage("baby.countBabysForQuery", "baby.findBabysForQuery",babyQuery);
	}

	/**
	 * 根据主键获取manlostBaby
	 * 
	 * @param id
	 * @return manlostBaby
	 */
	public Baby findOneByPrimaryKey(Long id) {
		Baby manlostBabyDO = (Baby) getSqlMapClientTemplate()
				.queryForObject("baby.findByPrimaryKey", id);
		return manlostBabyDO;
	}

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return 受影响的行数
	 */
	public Integer deleteOneByPrimaryKey(Long id) {
		Integer rows = (Integer) getSqlMapClientTemplate().delete(
				"baby.deleteByPrimaryKey", id);
		return rows;
	}
	
	public List<Baby> listBabyByIds(List<Long> ids) {
		if (ids == null || ids.isEmpty()) {
			return Collections.<Baby> emptyList();
		}
		Map<String, List<Long>> param = new HashMap<String, List<Long>>();
		param.put("ids", ids);
		List<Baby> babyList = getSqlMapClientTemplate()
				.queryForList("baby.listBabysByIds", param);
		return babyList == null ? Collections.<Baby> emptyList()
				: babyList;
	}

	@Override
	public Long insertBabies(List<Baby> babies) {
		return getSqlMapClientTemplate().batchInsert("baby.insert", babies);
	}
}